package com.syqc.monitor.ui.photo

import androidx.recyclerview.widget.LinearLayoutManager
import com.syqc.adapter.PhotosHistoryAdapter
import com.syqc.comlib.base.BaseRecycleActivity
import com.syqc.comlib.utils.ActivityResultUtil.launchAct
import com.syqc.comlib.utils.ActivityResultUtil.registerResultOk
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.monitor.databinding.ActivityToolbarListBinding
import com.syqc.utils.DataUtil
import com.syqc.utils.Keys

/**
 *@Author Jasper
 *@Time   2020/12/10
 *@Desc   照片列表
 */
class PhotoInfoActivity :
    BaseRecycleActivity<ActivityToolbarListBinding>(ActivityToolbarListBinding::inflate) {
    private var title = ""
    private val mAdapter by lazy { PhotosHistoryAdapter(this) }

    override fun initView() {
        super.initView()
        title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""

        ToolbarHelper.setToolbar(this, title)

        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.adapter = mAdapter
        mAdapter.setList(DataUtil.mPhotoInfo)
    }

    override fun setListener() {
        mAdapter.setOnItemClickListener { _, _, position ->
            launcher.launchAct<PhotoDetailActivity>(this) {
                putExtra(Keys.INTENT_TITLE, title)
                putExtra(Keys.INTENT_OTHER, position)
            }
        }
    }

    private val launcher = registerResultOk { result ->
        result.data?.let {
            val index = it.getIntExtra(Keys.INTENT_OTHER, 0)
            mRecyclerVB.recyclerView.scrollToPosition(index)
            mAdapter.index = index
            mAdapter.notifyDataSetChanged()
        }
    }
}